home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / imisc.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  3KB  |  104 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9. #include "ipredef.h"
  10. #include "time.h"
  11. #include "imiscp.h"
  12.  
  13. #ifdef MONITOR
  14. extern int type_of_delay;
  15. extern long Run_Total_Time;
  16. extern int seconds_per_tick;
  17. #endif
  18.  
  19. /* Procedures reset_clock and itime are used to maintain the 'clock'.
  20.  * reset_now resets the clock, itime returns the number of milliseconds
  21.  * since the clock was reset. Code is include to make it appear as though
  22.  * the clock was reset at the most recent midnight, in order that the
  23.  * addition of TIME and DURATION values can account for the
  24.  * overflow that occurs at midnight.
  25.  */
  26.  
  27. static long since_midnight;    /* milliseconds since midnight at start */
  28. static long start_time; /* starting time (units vary according to version) */
  29.  
  30. /* set TIME_KIND to determine which itime and reset_clock to use */
  31.  
  32. #define TIME_PC
  33. /* start_time is in 'ticks' elapsed since midnight, where there are 
  34.  * CLK_TCK ticks per second.
  35.  * To avoid overflow, we use ms_per_tick, milliseconds per tick. This
  36.  * assumes (as is currently the case) that CLK_TCK divides 1000
  37.  * evenly.
  38.  */
  39. #define ms_per_tick (1000 / CLK_TCK)
  40.  
  41.  
  42. void reset_clock()                                                                  /*;reset_clock*/
  43. {
  44.         /* set start_time and since_midnight as described above. */
  45. #ifdef GWUMON
  46.     if ( type_of_delay == 1 )
  47.         start_time = 0;
  48.         else
  49.         start_time = clock() * (long) ms_per_tick; /* milliseconds since midnight */
  50. #else
  51.         start_time = clock() * (long) ms_per_tick; /* milliseconds since midnight */
  52. #endif
  53.         since_midnight = start_time;
  54. }
  55.  
  56. #ifndef GWUMON
  57. long itime()                             /*;itime*/
  58. {
  59.         /* find elapsed seconds, convert to milliseconds, and add offset 
  60.      *  for midnight corresponding to desired origin
  61.      */
  62.         long  itim;
  63.         clock_t time_now;
  64.         time_now = clock() * (long) ms_per_tick;
  65.         /* adjust for passing midnight if necessary */
  66.         if (time_now < start_time) time_now = time_now + 86400000L ;
  67.         itim =   (long) (time_now - start_time);
  68.         return itim + since_midnight;
  69. }
  70. #endif
  71.  
  72. #ifdef GWUMON
  73.  
  74. /************************************************************************/
  75. /*    Procedure:    itime()                        */
  76. /*    Purpose:        This routine is a rewrite of the itime routine  */
  77. /*            in NYU AdaEd to allow for delays which are     */
  78. /*            dependent on the machine clock cycle (pseudo    */
  79. /*            time), not real time                */
  80. /************************************************************************/
  81.  
  82. long itime()                             /*;itime*/
  83. {
  84.         long  itim;
  85.         /* find elapsed seconds, convert to milliseconds, and add offset 
  86.      *  for midnight corresponding to desired origin
  87.      */
  88.     if ( type_of_delay == 1 )
  89.     {
  90.         itim = (long) (( Run_Total_Time / seconds_per_tick ) * 1000 );
  91.         return itim;
  92.     }
  93.         else
  94.     {
  95.             clock_t time_now;
  96.             time_now = clock() * (long) ms_per_tick;
  97.             /* adjust for passing midnight if necessary */
  98.             if (time_now < start_time) time_now = time_now + 86400000L ;
  99.             itim =   (long) (time_now - start_time);
  100.             return itim + since_midnight;
  101.     }
  102. }
  103. #endif
  104.